package com.yanqu.road.server.cmd.cookboy;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.cookboy.CookBoyFood;
import com.yanqu.road.entity.activity.cookboy.MakeFoodRes;
import com.yanqu.road.pb.cookboy.CookBoyProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.cookboy.CookBoyActivityMgr;
import com.yanqu.road.server.manger.activity.cookboy.CookBoyMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_COOK_BOY_MAKE_FOOD, desc = "切菜")
public class CookBoyMakeFoodCmd extends AbstractCommand {

	int activityId;
	int times;
	MakeFoodRes mkRes;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CookBoyProto.CookBoyMakeFoodReqMsg reqMsg = CookBoyProto.CookBoyMakeFoodReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		times = reqMsg.getTimes();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = CookBoyActivityMgr.getInTimeActivityInfo(activityId);
		if(activityInfo == null){
			ret = GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME;
			return;
		}
		if(times <= 0){
			ret = GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME;
			return;
		}
		mkRes = CookBoyMgr.makeFood(activityId,player.getUserId(),times);
		ret = mkRes.getRet();

	}

	@Override
	public void sendData(GamePlayer player) {
		CookBoyProto.CookBoyMakeFoodRespMsg.Builder respMsg = CookBoyProto.CookBoyMakeFoodRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(ret == 0){
			for(CookBoyFood cookBoyFood : mkRes.getMakeList()){
				CookBoyProto.CookBoyFoodTempMsg.Builder tempMsg = CookBoyProto.CookBoyFoodTempMsg.newBuilder();
				tempMsg.setFraction(cookBoyFood.getFraction());
				tempMsg.setLevel(cookBoyFood.getLevel());
				tempMsg.setPos(cookBoyFood.getPos());
				respMsg.addFoodList(tempMsg);
			}
		}
		player.sendPacket(Protocol.U_COOK_BOY_MAKE_FOOD, respMsg);
	}
}
